<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { statisticsFunnelInfo } from '@/api/charts'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '80%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      funnelInfo: [
        { 'screenCount': '筛选简历' },
        { 'firstScreenCount': '初筛通过' },
        { 'inviteViewCount': '邀约面试' },
        { 'joinViewCount': '参加面试' },
        { 'simulationCount': '拟录用' },
        { 'offerCount': '已发offer' },
        { 'entryCount': '已入职' }
      ]
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
    statisticsFunnelInfo().then(response => {
      const xaxisData = []
      const mixData = []
      this.funnelInfo.forEach(item => {
        const tg = Object.keys(item)[0]
        xaxisData.push(item[tg])
        mixData.push({ value: response.result[tg], name: item[tg] })
      })
      console.log(mixData)
      this.initChart(xaxisData, mixData)
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(xaxisData, mixData) {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c}人'
        },
        legend: {
          data: xaxisData
        },
        series: [
          {
            name: '漏斗图',
            type: 'funnel',
            left: '10%',
            top: 60,
            center: ['25%', '25%'], // for pie
            // x2: 80,
            bottom: 20,
            width: '80%',
            // height: {totalHeight} - y - y2,
            sort: 'descending',
            gap: 2,
            label: {
              show: true,
              formatter: '{b} {c}人'
            },
            data: mixData
          }
        ]
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  div{
    margin:0 auto;
  }
</style>
